<?php
class PhankenhsController extends AppController {

	var $name = 'Phankenhs';
	var $uses= array('Phankenh','Loaikenh','Nhanvien','Zdv','Z1xemkenh','Z2xemkenh','Z3xemkenh');
	var $helpers=array('Js');
	var $components = array('RequestHandler');
	
	function index($donvi_id=null)
	{
		if($donvi_id!=null)
		{
			$this->Phankenh->recursive = 0;
			$this->set('phankenhs', $this->paginate());
		}
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid phankenh', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('phankenh', $this->Phankenh->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Phankenh->create();
			if ($this->Phankenh->save($this->data)) {
				$this->Session->setFlash(__('The phankenh has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phankenh could not be saved. Please, try again.', true));
			}
		}
		$kenhs = $this->Phankenh->Kenh->find('list');
		$nhanviens = $this->Phankenh->Nhanvien->find('list');
		$this->set(compact('kenhs', 'nhanviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid phankenh', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Phankenh->save($this->data)) {
				$this->Session->setFlash(__('The phankenh has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The phankenh could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Phankenh->read(null, $id);
		}
		$kenhs = $this->Phankenh->Kenh->find('list');
		$nhanviens = $this->Phankenh->Nhanvien->find('list');
		$this->set(compact('kenhs', 'nhanviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for phankenh', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Phankenh->delete($id)) {
			$this->Session->setFlash(__('Phankenh deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Phankenh was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function xemkenhnv($donvi_id=null,$nhanvien_id=0,$loaikenh_id=0)
	{
		$isAjax =0;
		if($this->RequestHandler->isAjax())
		{
			
			$this->layout='ajax';
			$nhanvien_id =$this->data['Phankenh']['nhanvien'];
        	$loaikenh_id =$this->data['Phankenh']['loaikenh'];
			if ($nhanvien_id!='')
			{
				$this->Phankenh->recursive = 0;
				$this->set('phankenhs', $this->paginate(array('Kenh.loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>null)));
				$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
			
			}
			$isAjax =1;
			$this->set(compact('nhanvien_id','loaikenh','loaikenh_id','isAjax','donvi_id'));
		}
		else
		{
			if(!$donvi_id)
				$this->redirect(array('controller'=>'pages','action'=>'home'));
			$this->Phankenh->recursive = 0;
			$this->set('phankenhs', $this->paginate(array('Kenh.loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>null)));
			$nhanviens = $this->Phankenh->Nhanvien->find('list',array(
															'conditions'=>array('donvi_id'=>$donvi_id)));
			$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
			$this->set(compact('nhanviens','nhanvien_id','loaikenh','donvi_id','loaikenh_id','isAjax'));
		}
	}
	    
	function themkenhnv($donvi_id=null,$loaikenh_id=null,$nhanvien_id=0)
	{
		if ($donvi_id!=null && $loaikenh_id!=null)
		{
			$nhanviens = $this->Phankenh->Nhanvien->find('list',array(
															'conditions'=>array('donvi_id'=>$donvi_id)));
			//$nv = array_keys($nhanviens);
				$kenhdaco=$this->Phankenh->find('list',array(
										'recursive'=>0,
										'conditions'=>array('Kenh.donvi_id'=>$donvi_id,'Kenh.loaikenh_id'=>$loaikenh_id,
                                                             'ngaykt_hd'=>null),
										'fields'=>array('kenh_id')));
			//,'NOT'=>array('Kenh.id'=>$kenhdaco)
			$kenhs = $this->Phankenh->Kenh->find('all',array(
													'conditions'=>array('loaikenh_id'=>$loaikenh_id,
																		'donvi_id'=>$donvi_id,
																		'NOT'=>array('Kenh.id'=>$kenhdaco))));
			//$nhanvieninfo = $this->Phankenh->Nhanvien->find('list',array(
			//												'conditions'=>array('id'=>$nhanvien_id)));
			if (empty($kenhs))
			{
				$this->Session->setFlash(__('Không Có Kênh Bán Hàng Để Thêm Cho Nhân Viên Này', true));
				$this->redirect(array('action' => 'chonloaikenh',$donvi_id));
			}
			$this->set(compact('nhanvien_id','loaikenh','donvi_id','kenhs','loaikenh_id','nhanvieninfo','nhanviens'));
		}
		else
		{
			$this->redirect(array('controller'=>'pages' ,'action' => 'home'));
		}
	}

	function chonloaikenh($donvi_id=null)
	{
		
		if ($donvi_id!=null)
		{
            
			$loaikenhs = $this->Loaikenh->find('all');
			$this->set(compact('loaikenhs','donvi_id','nhanviens'));
		}
	}

	function luu($loaikenh_id=null)
	{
		if (!empty($this->data))
		{
			$count = $this->data['Phankenh']['count'];
			$kenhdaco = array();
			for($i=1;$i<=$count;$i++)
			{
				$nhanvien_id=$this->data['Phankenh']['nhanvien'];
				$kenh_id=$this->data['Phankenh']['checkbox'.$this->data['Phankenh']['checkbox_id'.$i]];
				if ($kenh_id!=0)
				{
					$data['Phankenh']['nhanvien_id']=$nhanvien_id;
					$data['Phankenh']['kenh_id']= $kenh_id;
					$data['Phankenh']['ngaybd_hd']=date('Y-m-d',strtotime($this->data['Phankenh']['ngaybd_hd']));
					$this->Phankenh->saveAll($data);
					/*$danghd=$this->Phankenh->find('list',array(
												'conditions'=>array('nhanvien_id'=>$nhanvien_id,
																	'kenh_id'=>$kenh_id),
												'fields'=>array('ngaybd_hd','ngaykt_hd')));
					if (!empty($danghd))
						if(!empty($danghd[key($danghd)])){}
							//$this->Phankenh->saveAll($data);
						else{}
							//$kenhdaco[]=$kenh_id;
					else{}
						//$this->Phankenh->saveAll($data);*/
				}		
			}
			$donvi_id = $this->Session->read('cur_dv');
			$this->Session->setFlash(__('Đã Lưu', true));
			$this->redirect(array('action' => 'chonloaikenh',$nhanvien_id));
			//$this->redirect(array('action' => 'xemkenhnv',$donvi_id,$nhanvien_id));
		}
		else
		{
			$this->redirect(array('controller'=>'pages' ,'action' => 'home'));
		}
	}

	function kenhnv()
	{
		$this->layout='ajax';
		$nhanvien_id =$this->data['Phankenh']['nhanvien'];
        $loaikenh_id =$this->data['Phankenh']['loaikenh'];
		if ($nhanvien_id!='')
		{
				$this->Phankenh->recursive = 0;
				$this->set('phankenhs', $this->paginate(array('Kenh.loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,'ngaykt_hd'=>null)));
				$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
			
		}
		$this->set(compact('nhanvien_id','loaikenh','loaikenh_id'));
	}
    
	function kenhnvexcel($nhanvien_id,$loaikenh_id)
	{
		$this->layout='';
		//$nhanvien_id =$this->data['Phankenh']['nhanvien'];
        $n=$this->Nhanvien->find('first',array('conditions'=>array('id'=>$nhanvien_id),
                'fields'=>array('donvi_id','manv','tennv'),'recursive'=>-1));
        $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$n['Nhanvien'] ['donvi_id']),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
        $tenloai=$this->Loaikenh->find('first',array(
                'conditions'=>array('id'=>$loaikenh_id),
                'fields'=>array('loaikenh'),'recursive'=>-1));
		if ($nhanvien_id!='')
		{
				$this->Phankenh->recursive = 0;
				$this->set('phankenhs', $this->Phankenh->find('all',
                    array('conditions'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>
                            $nhanvien_id,'ngaykt_hd'=>null))));
				$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
			
		}
		$this->set(compact('nhanvien_id','loaikenh','n','tendv','tenloai'));
	}
    
    function kenhdbexcel($nhanvien_id=null,$loaikenh_id=null)
	{
		$this->layout='';
		//$nhanvien_id =$this->data['Phankenh']['nhanvien'];
        $n=$this->Nhanvien->find('first',array('conditions'=>array('id'=>$nhanvien_id),
                'fields'=>array('donvi_id','manv','tennv'),'recursive'=>-1));
        $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$n['Nhanvien'] ['donvi_id']),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
        $tenloai=$this->Loaikenh->find('first',array(
                'conditions'=>array('id'=>$loaikenh_id),
                'fields'=>array('loaikenh'),'recursive'=>-1));
		if ($nhanvien_id!='')
		{
                $ngaybd = date('Y-m-d',strtotime($this->data['Kenh']['ngaybd']));
                $ngaykt = date('Y-m-d',strtotime($this->data['Kenh']['ngaykt']));
                 //cong tac diem ban
                $this->z1xemkenh->recursive = 0;
				$this->set('z1xemkenhs', $this->Z1xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,
                                                                 'ngaytao between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
                //thong tin thi truong
                $this->z2xemkenh->recursive = 0;
				$this->set('z2xemkenhs', $this->Z2xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,
                                                                 'ngaytao between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
                //tan suat cham soc
                $this->z3xemkenh->recursive = 0;
				$this->set('z3xemkenhs', $this->Z3xemkenh->find('all',
                            array('conditions'=>array('and'=>array('loaikenh_id'=>$loaikenh_id,'nhanvien_id'=>$nhanvien_id,
                                                                 'ngaydi between ? and ?'=>array($ngaybd,$ngaykt))),
                                )));
				$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
		}
		$this->set(compact('nhanvien_id','loaikenh','n','tendv','tenloai','ngaybd','ngaykt'));
	}
    
	function dunghd($nhanvien_id=null)
	{
		$donvi_id = $this->Session->read('cur_dv');
		if($nhanvien_id!=null)
		{
			if (!empty($this->data))
			{
				$kenh_id=$this->data['kenh'];
				$pk = $this->Phankenh->find('list',array('conditions'=>array('nhanvien_id'=>$nhanvien_id,'kenh_id'=>$kenh_id,'ngaykt_hd'=>null)));
				if (!empty($pk))
				{
					$ngaykt_hd= date('Y-m-d');
					$this->Phankenh->id=key($pk);
					$this->Phankenh->saveField('ngaykt_hd',$ngaykt_hd);
					$this->Session->setFlash(__('Đã Thực Hiên', true));
				}
				else 
				{
					$this->Session->setFlash(__('Không Thực Hiện Được. Vui Lòng Làm Lại', true));
				}
			}
			$loaikenh = $this->Loaikenh->find('list',array('fields'=>array('loaikenh')));
			$this->set(compact('loaikenh','nhanvien_id','donvi_id'));
		}
		else
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
	}

	function dungkenh($donvi_id,$nhanvien_id=null,$loaikenh_id=null)
	{
		$this->Nhanvien->displayField='tennv';
		$nhanviens = $this->Nhanvien->find('list',array('conditions'=>array('donvi_id'=>$donvi_id)));
		$this->Loaikenh->displayField = 'loaikenh';
		$loaikenhs = $this->Loaikenh->find('list');
		if($nhanvien_id==null)
				$nhanvien_id = key($nhanviens);
		if($loaikenh_id==null)
				$loaikenh_id = key($loaikenhs);
				
		$isAjax=0;	
		if($this->RequestHandler->isAjax())
		{
			$isAjax=1;
			$textsearch = $this->data['Phankenh']['textsearch'];
			$nhanvien_id = $this->data['Phankenh']['nhanvien_id'];
			$loaikenh_id = $this->data['Phankenh']['loaikenh_id'];
			if($textsearch==null)
			{
				$this->paginate = array('conditions'=>array('nhanvien_id'=>$nhanvien_id,'Kenh.loaikenh_id'=>$loaikenh_id,'ngaykt_hd'=>null),
								'fields'=>array('Kenh.*','Phankenh.id'));				
			}
			else
			{
				$this->paginate = array('conditions'=>array('nhanvien_id'=>$nhanvien_id,'Kenh.loaikenh_id'=>$loaikenh_id,'ngaykt_hd'=>null,'Kenh.tenkenh LIKE'=>'%'.$textsearch.'%'),
								'fields'=>array('Kenh.*','Phankenh.id'));
			}
			$this->Phankenh->unbindModel(array('hasMany'=>array('Kqbanhang')),false);
			
			$kenhs =$this->paginate();			
		}
		else
		{
			
			$this->Phankenh->unbindModel(array('hasMany'=>array('Kqbanhang')),false);
			$this->paginate = array('conditions'=>array('nhanvien_id'=>$nhanvien_id,'Kenh.loaikenh_id'=>$loaikenh_id,'ngaykt_hd'=>null),
								'fields'=>array('Kenh.*','Phankenh.id'));
			$kenhs =$this->paginate();			
		}
		
		$this->set(compact('kenhs','nhanviens','loaikenhs','nhanvien_id','loaikenh_id','donvi_id','isAjax'));
		
	}

	function luudungkenh()
	{
		$pkid = $this->data['Phankenh']['pkid'];
		foreach ($pkid as $id=>$check)
		{
			if($check==1)
			{
				$ngaykt_hd= date('Y-m-d');
				$this->Phankenh->id=$id;
				$this->Phankenh->saveField('ngaykt_hd',$ngaykt_hd);
			}
		}
	}
	
	function laykenh($nhanvien_id=null)
	{
		$this->layout='ajax';
		if ($nhanvien_id!=null)
		{
			$loaikenh_id = $this->data['Phankenh']['loaikenh'];
			$kenhs=$this->Phankenh->find('list',array(
										'recursive'=>0,'contain'=>array('Kenh'),
										'conditions'=>array('Kenh.loaikenh_id'=>$loaikenh_id,
															'nhanvien_id'=>$nhanvien_id,
															'ngaykt_hd'=>null),
										'fields'=>array('Kenh.id','Kenh.tenkenh')));
			$this->set(compact('kenhs'));
		}
		else
				$this->redirect(array('controller'=>'pages','action'=>'home'));
	}
    
	function autocompletekenh()
	{
		$this->layout='ajax';
		
		$tenkenh = strtolower($this->params['url']['term']);
		$nhanvien_id = strtolower($this->params['url']['nhanvien_id']);
		$loaikenh_id = strtolower($this->params['url']['loaikenh_id']);
		if (!$makenh) return;
		$kenhs = $this->Phankenh->find('list',array(
											'recursive'=>0,
											'fields'=>array('Kenh.id','Kenh.makenh'),
											'conditions'=>array('Nhanvien_id'=>$nhanvien_id,																
																'Kenh.loaikenh_id'=>$loaikenh_id,
																'ngaykt_hd'=>null,
																'Kenh.tenkenh LIKE'=>'%'.$tenkenh.'%')));
	
	$result = array();
	foreach ($kenhs as $key=>$value)
		array_push($result, array("id"=>$key, "value" => $value));
		
	$result=array_to_json($result);
	$this->set(compact('result'));
	}
}

	function array_to_json( $array )
	{

	    if( !is_array( $array ) ){
	        return false;
	    }
	
	    $associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
	    if( $associative ){
	
	        $construct = array();
	        foreach( $array as $key => $value ){
	
	            // We first copy each key/value pair into a staging array,
	            // formatting each key and value properly as we go.
	
	            // Format the key:
	            if( is_numeric($key) ){
	                $key = "key_$key";
	            }
	            $key = "\"".addslashes($key)."\"";
	
	            // Format the value:
	            if( is_array( $value )){
	                $value = array_to_json( $value );
	            } else if( !is_numeric( $value ) || is_string( $value ) ){
	                $value = "\"".addslashes($value)."\"";
	            }
	
	            // Add to staging array:
	            $construct[] = "$key: $value";
	        }
	
	        // Then we collapse the staging array into the JSON form:
	        $result = "{ " . implode( ", ", $construct ) . " }";
	
	    } else { // If the array is a vector (not associative):
	
	        $construct = array();
	        foreach( $array as $value ){
	
	            // Format the value:
	            if( is_array( $value )){
	                $value = array_to_json( $value );
	            } else if( !is_numeric( $value ) || is_string( $value ) ){
	                $value = "'".addslashes($value)."'";
	            }
	
	            // Add to staging array:
	            $construct[] = $value;
	        }
	
	        // Then we collapse the staging array into the JSON form:
	        $result = "[ " . implode( ", ", $construct ) . " ]";
	    }
	
	    return $result;
	}